package com.water.devtool.ui.define;

import java.util.Map;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.water.devtool.define.ClassBean;
import com.water.devtool.define.EntityBean;

public class ClassEditComposite extends Composite {
	final static Logger log = LoggerFactory.getLogger(ClassEditComposite.class);

	private EntityBean entity;
	private Table table;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ClassEditComposite(Composite parent, int style) {
		super(parent, style);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.setBounds(182, 532, 98, 30);
		btnNewButton.setText("New Button");

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.setBounds(371, 532, 98, 30);
		btnNewButton_1.setText("New Button");

		Button btnNewButton_2 = new Button(this, SWT.NONE);
		btnNewButton_2.setBounds(571, 532, 98, 30);
		btnNewButton_2.setText("New Button");

		Button btnNewButton_3 = new Button(this, SWT.NONE);
		btnNewButton_3.setBounds(785, 532, 98, 30);
		btnNewButton_3.setText("确认");

		Button button = new Button(this, SWT.NONE);
		button.setBounds(975, 532, 98, 30);
		button.setText("取消");

	}

	protected void loadTableData() {
		table.removeAll();
		Map<String, ClassBean> configs = entity.getClassConfig();
		Set<String> keys = configs.keySet();
		for (String key : keys) {
			ClassBean b = configs.get(key);
			TableItem item = new TableItem(table, SWT.NONE);
			String[] param = new String[] { b.getLevel(), b.getClassName(), b.getPackageName() };
			item.setText(param);
		}

	}

	public void closeTab() {
		TabFolder tabFolder = (TabFolder) getParent();
		TabItem[] items = tabFolder.getSelection();
		for (TabItem tabItem : items) {
			tabItem.dispose();
		}
	}

	// 创建表格
	private void createTable() {
		// 表格布局
		GridData gridData = new org.eclipse.swt.layout.GridData();
		gridData.horizontalAlignment = SWT.LEFT;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = SWT.FILL;

		// 创建表格，使用SWT.FULL_SELECTION样式，可同时选中一行
		table = new Table(this, SWT.FULL_SELECTION);
		table.setBounds(10, 10, 1151, 582);
		table.setHeaderVisible(true);// 设置显示表头
		table.setLinesVisible(true);// 设置显示表格线/*
		// 创建表头的字符串数组
		String[] tableHeader = { "层", "类名", "包名" };
		int[] widths = { 100, 100, 100, 100, 100 };
		for (int i = 0; i < tableHeader.length; i++) {
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText(tableHeader[i]);
			// 设置表头可移动，默认为false
			tableColumn.setMoveable(false);
			tableColumn.setWidth(widths[i]);
		}
		loadTableData();

		final TableEditor editor = new TableEditor(table);
		table.addMouseListener(new MouseAdapter() {

			public void mouseDown(MouseEvent event) {
				Control old = editor.getEditor();
				if (old != null) {
					old.dispose();
				}
				Point point = new Point(event.x, event.y);
				TableItem item = table.getItem(point);
				if (item != null) {
					int column = -1;
					for (int i = 1; i < table.getColumnCount(); i++) {
						Rectangle rect = item.getBounds(i);
						if (rect.contains(point)) {
							column = i;
						}
					}

					log.debug("选中第{}行的第{}列", table.getSelectionIndex(), column);
					final int col1 = column;
					final Text text = new Text(table, SWT.NONE);
					String str = item.getText(col1);
					text.setText(str);
					text.setFocus();
					text.setSelection(str.length());
					editor.setEditor(text, item, col1);
					text.addModifyListener(new ModifyListener() {

						public void modifyText(ModifyEvent event) {
							editor.getItem().setText(col1, text.getText());
						}
					});
				} else {// 如果选中空的一行，字段增加
					item = new TableItem(table, SWT.NONE);
					item.setText(new String[] { "字段名", "java名称", "类型" });

				}

			}
		});

		// ******************************************************/
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setEntity(EntityBean entity) {
		this.entity = entity;
		createTable();
	}
}
